package com.hk.shop.base.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hk.shop.base.dao.BaseDao;
import com.hk.shop.page.Page;
import com.hk.shop.page.PageInfo;



public abstract class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID>{

	

	private BaseDao<T, ID> baseDao;
	
	public BaseDao<T, ID> getBaseDao() {
		return baseDao;
	}
	
	@Autowired
	public void setBaseDao(BaseDao<T, ID> baseDao) {
		this.baseDao = baseDao;
	}

	

	

	@Override
	@Transactional(propagation=Propagation.REQUIRED)//默认为REQURED, 另外@Transactional注解好像不能被继承
	public T save(T t) throws Exception{
		return baseDao.save(t);
	}
	
	
	@Override
	@Transactional
	public T saveOrUpdate(T t) throws Exception{
		return baseDao.save(t);
	}

	@Override
	@Transactional
	public void delete(T t) throws Exception{
		baseDao.delete(t);
	}
	
	
	@Transactional
	public void delete(ID... ids) throws Exception {
		if (ids != null) {
			for (ID id : ids) {
				delete(baseDao.find(id));
			}
		}
	}

	@Override
	@Transactional
	public void update(T t) throws Exception{
		baseDao.update(t);
	}

	@Override
	@Transactional(readOnly = true)
	/*@Cacheable("userCache")*/
	public T find(ID id) throws Exception{
		return baseDao.find(id);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<T> findList(PageInfo pageInfo) throws Exception{
		return baseDao.findPage(pageInfo);
	}
	
	
	@Override
	@Transactional(readOnly = true)
	public List<T> findAll() throws Exception{
		return baseDao.findAll();
	}

	@Override
	@Transactional(readOnly = true)
	public T findUniqueByProperty(String propertyName, Object value) throws Exception{
		return baseDao.findUniqueByProperty(propertyName, value);
	}
	
	@Override
	@Transactional(readOnly = true)
	public long getTotalNum(PageInfo pageInfo) throws Exception {
		return baseDao.queryTotalNum(pageInfo);
	}
	

}
